//
//  HYFComposeTextView.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/27.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

class HYFComposeTextView: UITextView {
    
    //重写父类已有属性,监听外面 textView 字号赋值是多少,然后调整占位文字的字号大小
     override var font:UIFont?{
        
        didSet{
            
            placeHolderLabel.font = font
        }
        
    }
    
    //设置占位文字
    var placeHolder:String?{
        
        didSet{
            
            placeHolderLabel.text = placeHolder
        }
        
    }
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
       setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        //添加控件,设置约束
        addSubview(placeHolderLabel)
        
        placeHolderLabel.snp_makeConstraints { (make) in
            
            make.leading.equalTo(self).offset(5)
            make.top.equalTo(self).offset(8)
            make.width.equalTo(SCREENW - 10)
        }
        
        //监听文字的改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "valueChanged", name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    
    func valueChanged(){
        
        placeHolderLabel.hidden = self.hasText()
    }
    
    //懒加载控件
    private lazy var placeHolderLabel:UILabel = {
        
       let lab = UILabel()
        lab.numberOfLines = 0
        lab.textColor = UIColor.grayColor()
        
        return lab
    }()
    

}
